# -*- coding: utf-8 -*-
# @Time    : 2022/4/13 16:00
# @Author  : zyw
# @Email   : panda_kings@163.com
# @File    : comment.py
from flask import Blueprint,jsonify,g
from flask_restful import Api,Resource,reqparse

from common.models import db
# from common.models.news import Comment,NewsInfo

cmp_bp = Blueprint('comment',__name__, url_prefix='/comment')

api = Api(cmp_bp)


# class CommentRes(Resource):
#     def post(self):
#         """
#         添加评论
#         用户的评论分为两种, 一种是对资讯的评论, 另一种是针对于评论内容的回复性评论.
#         两种都属于评论, 使用同一个接口, 根据参数的不同来区分评论的种类. 将评论的数据发送至后端, 后端获取到数据后, 根据参数的不同进行判断, 然后进行存储.
#         """
#         parse = reqparse.RequestParser()
#         parse.add_argument('target')
#         parse.add_argument('content')
#         parse.add_argument('target')
#         args = parse.parse_args()
#         art_id = args['art_id'] # art_id不存在代表的是对文章的评论,存在是对评论的回复
#         content = args['content']
#         target = args['target']
#
#         if not art_id:
#             # 若不存在,是对文章的的评论
#             # 此时target是文章的id,
#             if not content:
#                 return jsonify({'msg':"评论内容为空"})
#             comment = Comment(user_id=g.user_id,article_id=target,content=content)
#             db.session.add(comment)
#             db.session.commit()
#             # 返回
#             cmid = comment.cmid
#             data_ = {'com_id':cmid,"target":target, 'art_id':target}
#             return jsonify({'msg':"评论文章成功",'data':data_})
#         else:
#             # 判断art_id, 若art_id 存在, 说明是对文章评论的回复
#             if not content:
#                 return jsonify({'msg':"评论内容为空"})
#             # 此时target是被回复的评论的id
#             comment = Comment(user_id=g.user_id,parent_id=target,content=content)
#             db.session.add(comment)
#             db.session.commit()
#             # 此时不一样target 是被回复的评论id, art_id,是文章
#             data = {'com_id':comment.cmid, "target":target, "art_id":art_id}
#             return jsonify({"msg":"回复成功","data":data})


# class CommentListResource(Resource):
#     def get(self):
#         """获取评论"""
#         req = reqparse.RequestParser()
#         req.add_argument('type') # type 的值为a 代表评论的是文章,值是c代表是回复的评论
#         req.add_argument('source') # source 值是id
#         req.add_argument('offset')
#         req.add_argument('limit')
#         args = req.parse_args()
#         if args.type == 'a':
#             # 此时说明获取的是文章的评论,此时source 值是文章的id
#             article_id = args.get('source')
#             # 要根据文章的id获取文章以及评论
#             comment = Comment.query.filter_by(article_id=article_id)
#             # 获取文章的评论量
#             news = NewsInfo.query.filter_by(nid=article_id).first()
#             comment_count = news.comment_count()
#             # 判断文章是否有评论
#             if comment:
#                 # 此时文章所有的评论
#                 comments = comment.all()
#                 # 获取最后一个评论的id
#                 comment_last = comments[-1]
#                 end_id = comment_last.cmid if comment_last else None
#                 # 判断当前评论的id 是不是当前页的最后一个id, 若是最后一个id,就作为offset的参数,否则就为None
#                 # 使用分页和限制条件
#                 current_comment = comments.offset(args['offset']).limit(args['limit'])
#                 if current_comment:
#                     # 此时说明评论被分页
#                     last_id = current_comment[-1].cmid
#                 else:
#                     # 此时评论没有分页
#                     last_id = None
#                 result = []
#                 for comment in current_comment:
#                     # 获取评论的内容
#                     result.append({
#                         'com_id': comment.cmid,
#                         'aut_id': g.user_id,
#                         'aut_name': comment.user.account,
#                         'aut_photo': comment.user.profile_photo,
#                         'like_count': comment.like_count,
#                         'reply_count': comment.reply_count,
#                         'pubdate': comment.ctime.strftime("%Y-%m-%d"),
#                         'content': comment.content,
#                         'is_top': comment.is_top,
#                         'is_liking': 1,# 是否被点赞
#
#                     })
#                 return jsonify({'msg':"获取评论成功",'data':{'total_count':comment_count,
#                                                        'end_id':end_id,
#                                                        'last_id':last_id,
#                                                        'result':result}})
#
#             else:
#                 # 此时文章没有评论
#                 return {
#                     'message': 'ok',
#                     'data': {
#                         'total_count': 0,
#                         'end_id': None,
#                         'last_id': None,
#                         'results': []
#                     }
#                 }
#
#
#         else:
#             # 此时说明后获取的是评论的回复,此时source 值是评论的id
#             pass



# api.add_resource(CommentRes,'/addComment')
# api.add_resource(CommentListResource, '/commentInfo')




